package cn.tpshion.devops.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.unit.DataSizeUtil;
import cn.tpshion.devops.domain.vo.FileInfoVO;
import cn.tpshion.devops.service.OSSService;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.http.HttpMethodName;
import com.qcloud.cos.model.COSObjectSummary;
import com.qcloud.cos.model.ListObjectsRequest;
import com.qcloud.cos.model.ObjectListing;
import lombok.extern.slf4j.Slf4j;
import org.dromara.x.file.storage.core.FileStorageService;
import org.dromara.x.file.storage.core.platform.TencentCosFileStorage;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import javax.annotation.Resource;
import java.net.URL;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class OSSServiceImpl implements OSSService {

    @Resource
    private FileStorageService fileStorageService;

    @Override
    public List<FileInfoVO> list() {
        TencentCosFileStorage fileStorage = fileStorageService.getFileStorage();
        COSClient client = fileStorage.getClient();

        ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
        // 设置 bucket 名称
        listObjectsRequest.setBucketName(fileStorage.getBucketName());
        listObjectsRequest.setPrefix("app/");
        listObjectsRequest.setMaxKeys(1000);
        ObjectListing list = client.listObjects(listObjectsRequest);

        List<COSObjectSummary> summaryList = list.getObjectSummaries();
        if(ObjectUtils.isEmpty(summaryList)){
            return Collections.emptyList();
        }

        return summaryList.stream().map(i -> {
            FileInfoVO result = new FileInfoVO();
            result.setKey(i.getKey());
            result.setSize(DataSizeUtil.format(i.getSize()));
            result.setTime(DateUtil.formatDateTime(i.getLastModified()));
            result.setDate(i.getLastModified().getTime());
            return result;
        }).filter(i -> !i.getKey().equals("app/")).sorted(Comparator.comparingLong(FileInfoVO::getDate).reversed())
                .collect(Collectors.toList());
    }

    @Override
    public String download(FileInfoVO fileInfo) {
        TencentCosFileStorage fileStorage = fileStorageService.getFileStorage();
        COSClient client = fileStorage.getClient();
        Date expirationDate = new Date(System.currentTimeMillis() + 30 * 60 * 1000);
        HttpMethodName method = HttpMethodName.GET;
        URL url = client.generatePresignedUrl(fileStorage.getBucketName(), fileInfo.getKey(), expirationDate, method);
        return url.toString();
    }

    @Override
    public void delete(FileInfoVO fileInfo) {
        TencentCosFileStorage fileStorage = fileStorageService.getFileStorage();
        COSClient client = fileStorage.getClient();
        client.deleteObject(fileStorage.getBucketName(), fileInfo.getKey());
    }
}
